# coding=utf-8
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.staticfiles import StaticFiles
from pathlib import Path

from app.core.config import settings
from app.api.endpoints import router

app = FastAPI(
    title=settings.APP_NAME,
    description=settings.APP_DESCRIPTION,
    docs_url=None,  # 禁用默认的swagger文档
    redoc_url=None  # 禁用默认的redoc文档
)

# 添加静态文件支持
static_path = Path(__file__).parent / "static"
app.mount("/debug", StaticFiles(directory=str(static_path), html=True), name="debug")

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 挂载路由
app.include_router(router, prefix=settings.API_V1_STR)

@app.get("/", include_in_schema=False)
async def read_root():
    return get_swagger_ui_html(
        openapi_url="/openapi.json",
        title=app.title + " - API文档",
        swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.11.8/swagger-ui-bundle.js",
        swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.11.8/swagger-ui.css",
        swagger_favicon_url="https://fastapi.tiangolo.com/img/favicon.png"
    )
































